package com.songcha.library_business.proxy.database

import com.songcha.library_business.bean.book.BookDetailBean
import com.songcha.library_business.helper.DaoManagerHelper
import com.songcha.library_common.util.LogUtil
import com.songcha.library_database_douyue.bean.BookDetailDBBean
import com.songcha.library_database_douyue.greendao.BookDetailDBBeanDao
import com.songcha.library_database_douyue.manager.GreenDaoManager
import org.greenrobot.greendao.query.WhereCondition.StringCondition

class BookDetailDaoManager {
    companion object{

        @JvmStatic
        fun addBookDetail(bookDetail: BookDetailBean){
            val dao=GreenDaoManager.getInstance().bookDetailDBBeanDao
            val bookDetailDBBean=BookDetailDBBean().apply {
                bookId=bookDetail.id
                name=bookDetail.bookName
                thumb=bookDetail.thumb
                author=bookDetail.author
                catalog=bookDetail.catalog
                chapterCount=bookDetail.chapterCount
                textCount=bookDetail.textCount
                update=bookDetail.update
                desc=bookDetail.desc
                status=bookDetail.status
                isVip=bookDetail.isVip
                channelId=bookDetail.channelId
                cid=bookDetail.cid
            }
            try {
                dao.insertOrReplace(bookDetailDBBean)
            }catch (e:Exception){
                LogUtil.log("保存书籍详情异常")
                e.printStackTrace()
            }

        }

        @JvmStatic
        fun getBookdetailListByIdList(ids: MutableList<String>):MutableList<BookDetailDBBean>{
            //ORDER BY CASE BOOK_ID WHEN '8185' THEN 0 WHEN '8735' THEN 1 WHEN '8527' THEN 2 WHEN '8177' THEN 3 WHEN '8187' THEN 4 END
            if(ids.size==0) return mutableListOf()
            val dao=GreenDaoManager.getInstance().bookDetailDBBeanDao
            val qb=dao.queryBuilder()
            //order by in
            qb.where(StringCondition(BookDetailDBBeanDao.Properties.BookId.columnName+ " in "+DaoManagerHelper.getRangeStringByIds(ids)+" order by case "+BookDetailDBBeanDao.Properties.BookId.columnName+DaoManagerHelper.getWhenCaseStringByIds(ids)+"end"))

            val list= qb.list()

            if(list.size==ids.size)
                return list
            else{
                val tmp= mutableListOf<BookDetailDBBean>()
                for(i in 0 until ids.size){
                    val item=ids[i]
                    var bean= getBookDetailById(list, item)
                    if(bean==null){
                        bean= BookDetailDBBean()
                        bean!!.bookId=item
                    }
                    tmp.add(bean)
                }
                return tmp
            }
        }

        @JvmStatic
        private fun getBookDetailById(list:MutableList<BookDetailDBBean>,id:String):BookDetailDBBean?{
            for(item in list){
                if(item.bookId==id)
                    return item
            }
            return null
        }

    }
}